package com.hz.cheetah.sample.client;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.hz.cheetah.helper.Async;
import com.hz.cheetah.sample.api.HelloService;
import com.hz.cheetah.sample.api.HelloServiceAsync;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.CountDownLatch;

@ContextConfiguration(locations = {"classpath*:/spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class HelloClient {


    @Autowired()
    @Qualifier("HelloService")
    HelloService helloService;
    @Autowired()
    @Qualifier("HelloService2")
    HelloServiceAsync helloService2;

    @Test
    public void testStringg() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        String result = helloService2.hello("World");
        System.out.println("......"+result);

        ListenableFuture<String> result2 = helloService2.helloAsync("世界");
        System.out.println(result2.get());
        Futures.addCallback(result2, new FutureCallback<String>() {
            // we want this handler to run immediately after we push the big red button!
            public void onSuccess(String explosion) {
                System.out.println("&&&&&&&&&&&&&&"+explosion);
                latch.countDown();
            }
            public void onFailure(Throwable thrown) {
                thrown.printStackTrace(); // escaped the explosion!
                latch.countDown();
            }
        });
        latch.await();
    }
}
